package com.cloud.pole.shop.biz.order;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.cloud.pole.common.biz.BaseBiz;
import com.cloud.pole.mapper.goods.ShopGoodsMapper;
import com.cloud.pole.mapper.order.ShopOrderMapper;
import com.cloud.pole.shop.model.entity.order.ShopOrder;
import com.cloud.pole.shop.model.entity.order.ShopOrderAdmin;
import com.cloud.pole.shop.model.vo.order.ShopRecommendOrderVo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-08 16:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShopOrderBiz extends BaseBiz<ShopOrderMapper, ShopOrder> {

	@Cache(key="shopOrder{1}")
    public ShopOrder selectByPrimaryKey(String id) {
        return super.selectById(id);
    }
	
	@Override
	@CacheClear(key="shopOrder{1.orderSn}")
	public void updateSelectiveById(ShopOrder shopOrder) {
		super.updateSelectiveById(shopOrder);
	}

	/**
	 * 查询用户订单
	 * @param userId
	 * @param searchType
	 * @return
	 */
    public List<ShopOrder> selectOrderListByUserId(Long userId, Integer searchType) {
    	 Map<String, Object> map = new HashMap<String, Object>();
         map.put("userId", userId);
		 //待付款
		 if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_PAY) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_NO);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_DEFAULT);
		 }
		 //待发货
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_SEND) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_PAY_OK);
		 }
		 //待收货，需要兼容到付，需要特殊处理
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_RECEIVE) {
			 map.put("orderProcessed", ShopOrder.SEARCH_TYPE_USER_TO_BE_RECEIVED);
//			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
//			 map.put("orderStatus", ShopOrder.ORDER_STATUS_DELIVERED);
		 }
		 //已完成
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_FINISH) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_FINISH);
		 }
		 //（已完成）待评论
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_COMMENT) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_FINISH);
			 map.put("toBeCommented", 1);
		 }
		 map.put("deleteFlag", 0);
		 return mapper.selectPageOrderListByMap(map);
    }
    
    /**
     * 查询店铺订单
     * @param shopId
     * @param searchType
     * @return
     */
    public List<ShopOrder> selectOrderListByShopId(Integer shopId, Integer searchType) {
   	 Map<String, Object> map = new HashMap<String, Object>();
        map.put("shopId", shopId);
         //0：所有订单
         if(searchType.intValue() == ShopOrder.SEARCH_TYPE_SHOP_ALL) {
			 map.put("shopProcessed", ShopOrder.SEARCH_TYPE_SHOP_ALL);
		 }
		 //1：待处理 
         else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_SHOP_NEED_DEAL) {
			 map.put("shopProcessed", ShopOrder.SEARCH_TYPE_SHOP_NEED_DEAL);
		 }
		 //2:：已经处理
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_SHOP_PROCESSED) {
			 map.put("shopProcessed", ShopOrder.SEARCH_TYPE_SHOP_PROCESSED);
		 }
//		 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
		 return mapper.selectPageOrderListByMap(map);
   }
    
    /**
     * 查询店铺订单总数
     * @param shopId
     * @param searchType
     * @return
     */
    public int selectCountOrderByShopId(Integer shopId, Integer searchType) {
    	 Map<String, Object> map = new HashMap<String, Object>();
         map.put("shopId", shopId);
		 //待发货
		 if(searchType.intValue() == ShopOrder.SEARCH_TYPE_SHOP_NEED_DEAL) {
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_PAY_OK);
		 }
		 //已处理
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_SHOP_PROCESSED) {
			 map.put("shopProcessed", ShopOrder.SEARCH_TYPE_SHOP_PROCESSED);
		 }
		 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
		 return mapper.selectCountOrderListByMap(map);
    }
    

    
    /**
     * 统计数据
     * @param userId
     * @param searchType
     * @return
     */
    public int selectCountOrderByUserId(Long userId, Integer searchType) {
    	 Map<String, Object> map = new HashMap<String, Object>();
         map.put("userId", userId);
		 //待付款
		 if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_PAY) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_NO);
		 }
		 //待发货
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_SEND) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_PAY_OK);
		 }
		 //待收货，需要兼容到付，需要特殊处理
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_RECEIVE) {
			 map.put("orderProcessed", ShopOrder.SEARCH_TYPE_USER_TO_BE_RECEIVED);
//			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
//			 map.put("orderStatus", ShopOrder.ORDER_STATUS_DELIVERED);
		 }
		 //已完成
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_FINISH) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_FINISH);
		 }
		 //（已完成）待评论
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_COMMENT) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_FINISH);
			 map.put("toBeCommented", 1);
		 }
		 map.put("deleteFlag", 0);
		 
		 return mapper.selectCountOrderListByMap(map);
    }

	public List<ShopOrderAdmin> selectPageShopOrderAdminList(Map<String, Object> map) {
		return mapper.selectPageShopOrderAdminList(map);
	}
	
	public int addShopOrderShippingAddress(ShopOrder shopOrder) {
		return mapper.addShopOrderShippingAddress(shopOrder);
	}

	public List<ShopRecommendOrderVo> selectOrderListByRecommendId(Long userId, Integer searchType) {
		Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
		 //待付款
		 if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_PAY) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_NO);
		 }
		 //待发货
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_SEND) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_PAY_OK);
		 }
		 //待收货，需要兼容到付，需要特殊处理
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_RECEIVE) {
			 map.put("orderProcessed", ShopOrder.SEARCH_TYPE_USER_TO_BE_RECEIVED);
//			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
//			 map.put("orderStatus", ShopOrder.ORDER_STATUS_DELIVERED);
		 }
		 //已完成
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_FINISH) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_FINISH);
		 }
		 //（已完成）待评论
		 else if(searchType.intValue() == ShopOrder.SEARCH_TYPE_NEED_COMMENT) {
			 map.put("payStatus", ShopOrder.PAY_STATUS_YES);
			 map.put("orderStatus", ShopOrder.ORDER_STATUS_FINISH);
			 map.put("toBeCommented", 1);
		 }
		 map.put("deleteFlag", 0);
		return mapper.selectOrderListByRecommendId(map);
	}
}
